﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using YArchitech.Controls;
using YArchitech.LIB;

namespace YArchitech.Revit
{
	public partial class ViewFormSelect : System.Windows.Forms.Form
	{
		public ViewFormSelect(List<Autodesk.Revit.DB.View> views)
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = Resource.YArchitechIcon;
			this.initListView(views);
		}

		private void initListView(List<Autodesk.Revit.DB.View> views)
		{
			List<Autodesk.Revit.DB.View> list = new List<Autodesk.Revit.DB.View>();
			List<string> list2 = new List<string>();
			foreach (Autodesk.Revit.DB.View view in views)
			{
				ElementId typeId = view.GetTypeId();
				Element element = view.Document.GetElement(typeId);
				if (element != null)
				{
					string item = (element as ViewFamilyType).Name + "：" + RevitVersionFuncs.GetViewName(view);
					list2.Add(item);
				}
			}
			list2.Sort();
			foreach (string b in list2)
			{
				foreach (Autodesk.Revit.DB.View view2 in views)
				{
					ElementId typeId2 = view2.GetTypeId();
					Element element2 = view2.Document.GetElement(typeId2);
					if (element2 != null && (element2 as ViewFamilyType).Name + "：" + view2.Name == b)
					{
						list.Add(view2);
					}
				}
			}
			foreach (Autodesk.Revit.DB.View view3 in list)
			{
				ElementId typeId3 = view3.GetTypeId();
				Element element3 = view3.Document.GetElement(typeId3);
				if (element3 != null)
				{
					string name = (element3 as ViewFamilyType).Name;
					this.lbViews.Items.Add(new ListItem(name + "：" + RevitVersionFuncs.GetViewName(view3), view3.Id.ToString()));
				}
			}
			if (this.lbViews.Items != null && this.lbViews.Items.Count > 0)
			{
				this.lbViews.SelectedItem = this.lbViews.Items[0];
			}
		}

		private void btnOk_Click(object sender, EventArgs e)
		{
			if (this.lbViews.SelectedItem != null)
			{
				this.viewId = new ElementId(int.Parse((this.lbViews.SelectedItem as ListItem).Value));
				base.DialogResult = DialogResult.OK;
				return;
			}
			YJKMessageBox.Information("请选择视图！");
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private void lbViews_DoubleClick(object sender, EventArgs e)
		{
			if (this.lbViews.SelectedItem != null)
			{
				this.viewId = new ElementId(int.Parse((this.lbViews.SelectedItem as ListItem).Value));
				base.DialogResult = DialogResult.OK;
				return;
			}
			YJKMessageBox.Information("请选择视图！");
		}

		public ElementId viewId;
	}
}
